package com.tianqu.tms.www.attn.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.SynAttnBo;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.IntegralcardUpdateHistory;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.attn.viewhelper.AttnViewhelper;

/**
 * @author zhangxueliang
 * 经办人添加结果controller
 */
public class AttnCreationInputResultController extends TmsController{
	private Logger logger = Logger.getLogger(AttnCreationInputResultController.class);
	private static final String SYSPATH = "sysPath";//
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AttnViewhelper attnViewhelper = (AttnViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			User user = User.getInstance();
			TmsUserProfile profile = uc.getUserProfile();
			user.setLoginId(attnViewhelper.getLoginName());
			user.setRealName(attnViewhelper.getAttnName());
			user.setPassword(attnViewhelper.getPassword());
			user.setUcId("11");//默认系统会员
			user.setUpdateUserId(profile.getUniqueId());
			user.setInsertUserId(profile.getUniqueId());
			user.saveUserInfo(user,3);//插入user
			
			// 根据userId查询出用户的主键ID
			// 向权限表中添加一天权限信息
			user = user.getUserProfileByLoginid(attnViewhelper.getLoginName(),
					TmsConstants.BLANK);
			UserAuth auth = UserAuth.getInstance();

			// 根据userClass查询roleId
			UserRole role = UserRole.getInstance();
//			List<UserRole> roleList = role.getRoleinfoByUcId(Integer
//					.parseInt(user.getUcId()));

			
			auth.insertUserAuthInfo(user.getId(), 450);
			
			String sysPath = (String) uc.getAttribute(SYSPATH);
			//插入attn
			Attn attn = Attn.getInstance();
			Member mem = Member.getInstance();
			Sequence sequence = Sequence.getInstance();
			String attnId = "";
			attnId = sequence.getSerial(7);
			attn.setAttnId(attnId);
			attn.setAttnName(attnViewhelper.getAttnName());
			attn.setAttnAddress(attnViewhelper.getAttnAddress());	
			attn.setAttnIdCardNo(attnViewhelper.getAttnIdCardNo());
			attn.setAccessCardNo(attnViewhelper.getAccessCardNo());
			attn.setAccessCardId(attnViewhelper.getAccessCardId());
			attn.setAttnPhone(attnViewhelper.getAttnPhone());
			attn.setMemberId(String.valueOf(mem.getMemIdByMemName(attnViewhelper.getMemberId())));
			attn.setInsertUserId(profile.getUniqueId());
			attn.setUpdateUserId(profile.getUniqueId());
			attn.setAttnPhoto(attnViewhelper.getAttnPhoto());
			attn.setUserId(attnViewhelper.getLoginName());
			attn.insertAttn(attn,sysPath);
			
			if(!StringUtil.isNullOrBlank(attnViewhelper.getAccessCardId())){
				IntegralcardUpdateHistory history = IntegralcardUpdateHistory.getInstance();
				history = ToHistory(history,attnViewhelper);
				history.setInsertUserId(profile.getUniqueId());
				history.setMemberId(attnId);
				history.insertHistory(history);
			}
			
			controller.commit();
			
			//微信同步
			MobileMessage message = new MobileMessage();
			message.putValue("attnId",attnId);
			message.setMessage("TMS经办人录入功能调用[经办人添加]接口失败,经办人ID为#attnId#");
			SynAttnBo api = new SynAttnBo();
			api.setMobileMessage(message);
			api.setAttnId(attnId);
			api.setMemberId(attn.getMemberId());
			api.setAttnName(attn.getAttnName());
			api.setAccessCardNo(attn.getAccessCardNo());
			api.setAttnPhone(attn.getAttnPhone());
			api.setUserId(attn.getUserId());
			PullingExecutor.getInstance().submit(api);
			
		} catch (Exception e)
		{
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");	
	}
	/**
	 * view -> entity
	 * @param history
	 * @param view
	 * @return
	 */
	private IntegralcardUpdateHistory ToHistory(IntegralcardUpdateHistory history,AttnViewhelper view){
		history.setMemberType(2);
		history.setBusinessType(1);
		history.setAccessCardIdNew(view.getAccessCardId());
		history.setAccessCardIdOld(view.getAccessCardId());
		history.setAccessCardNoNew(view.getAccessCardNo());
		history.setAccessCardNoOld(view.getAccessCardNo());
		history.setChangePrice(0);
		history.setRemarks("");
		
		return history;
	}
}
